/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * HoaDon.java
 *
 * Created on Nov 11, 2012, 12:53:18 PM
 */
package doan;

import Dao.CTHoaDonDao;
import Dao.SanPhamDao;
import Pojo.CTHoaDonPojo;
import Pojo.SanPhamPojo;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author LETHOM
 */
public class CapNhatTrangThaiHD extends javax.swing.JFrame {

    /** Creates new form HoaDon */
    public CapNhatTrangThaiHD() {
        initComponents();
      
     jButton6.setVisible(false);
     jButton7.setVisible(false);
     jComboBox4.setVisible(false);
       LoadDSSP();
       LoadDSCTHD();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtSoLuong = new javax.swing.JTextField();
        txtGia = new javax.swing.JTextField();
        combDSSP = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        txtMaHoaDon = new javax.swing.JTextField();
        jButton6 = new javax.swing.JButton();
        jComboBox4 = new javax.swing.JComboBox();
        jButton7 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Cập nhật hóa đơn");

        jLabel2.setText("Danh Sách Sản Phẩm");

        jPanel1.setBackground(new java.awt.Color(153, 153, 153));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel3.setText("Mã sản phẩm");
        jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(24, 39, -1, -1));

        jLabel4.setText("Giá");
        jPanel1.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 110, -1, -1));

        jLabel5.setText("Số lượng");
        jPanel1.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 80, -1, -1));
        jPanel1.add(txtSoLuong, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 80, 110, -1));
        jPanel1.add(txtGia, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 110, 110, -1));

        jPanel1.add(combDSSP, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 36, -1, -1));

        jButton1.setText("Thêm");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 180, -1, -1));

        jButton2.setText("Sửa");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 180, -1, -1));

        jLabel10.setText("Mã HD:");
        jPanel1.add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 140, -1, -1));

        txtMaHoaDon.setName("txtMaHD"); // NOI18N
        jPanel1.add(txtMaHoaDon, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 140, 114, -1));

        jButton6.setText("Tìm kiếm");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton6, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 160, -1, -1));

        jComboBox4.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jPanel1.add(jComboBox4, new org.netbeans.lib.awtextra.AbsoluteConstraints(104, 264, 117, -1));

        jButton7.setText("Cập nhật");
        jButton7.setName("btnThem"); // NOI18N
        jPanel1.add(jButton7, new org.netbeans.lib.awtextra.AbsoluteConstraints(79, 317, -1, -1));

        jButton5.setText("thoat");

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "STT", "Mã sản phẩm", "Tên sản phẩm", "Số lượng", "Giá ", "Tổng tiền"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jTable2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jTable2MousePressed(evt);
            }
        });
        jTable2.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
            }
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
                jTable2CaretPositionChanged(evt);
            }
        });
        jScrollPane2.setViewportView(jTable2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 474, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton5))
                        .addGap(18, 18, 18)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 365, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(87, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(jLabel2)
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 227, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(33, 33, 33)
                        .addComponent(jButton5))
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(383, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
int selectedRow=-1;
   ArrayList< SanPhamPojo> dssp;
    void LoadDSSP(){
 dssp= Dao.SanPhamDao.DanhSachSanPham();
       for(int i=0;i<dssp.size();i++){

           combDSSP.addItem(dssp.get(i).getTenSP());

       }
    }


    ArrayList<CTHoaDonPojo>dsCTHD;
   void LoadDSCTHD(){
         dsCTHD=Dao.CTHoaDonDao.DSChiTietHoaDon();
       Vector<String>v;
       DefaultTableModel model=new DefaultTableModel();

       model.addColumn("STT");
       model.addColumn("Mã SP");
       model.addColumn("Tên SP");
       model.addColumn("Số lượng");
       model.addColumn("Giá bán");
       model.addColumn("Tổng tiền");
       jTable2.setModel(model);
       for(int i=0;i<dsCTHD.size();i++){
           v=new Vector<String>();
           CTHoaDonPojo ct=dsCTHD.get(i);
           v.add(String.valueOf(i));
           v.add(String.valueOf(ct.getMaSP()));
           SanPhamPojo sp=SanPhamDao.DanhSanPham(ct.getMaSP());
           v.add(sp.getTenSP());
           v.add(String.valueOf(ct.getSoLuong()));
           v.add(String.valueOf(ct.getGiaBan()));
           v.add(String.valueOf(ct.getGiaBan()*ct.getSoLuong()));
           model.addRow(v);

       }



   }
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
   CTHoaDonPojo ct=new CTHoaDonPojo();
   ct.setGiaBan(Double.parseDouble(txtGia.getText()));
   ct.setMaHoaDon(Integer.parseInt(txtMaHoaDon.getText()));
   int index=combDSSP.getSelectedIndex();
   SanPhamPojo sp=dssp.get(index);
   ct.setMaSP(sp.getMaSP());
   ct.setSoLuong(Integer.parseInt(txtSoLuong.getText()));
   CTHoaDonDao.ThemCTHD(ct);
   
    LoadDSCTHD();
    }//GEN-LAST:event_jButton1ActionPerformed

private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jButton6ActionPerformed

private void jTable2CaretPositionChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_jTable2CaretPositionChanged
// TODO add your handling code here:

    txtGia.setText(String.valueOf(6));
}//GEN-LAST:event_jTable2CaretPositionChanged

private void jTable2MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable2MousePressed
// TODO add your handling code here:

    JTable jtable=(JTable)evt.getSource();
    int selection=jtable.getSelectedRow();
    selectedRow=selection;
    CTHoaDonPojo ct=dsCTHD.get(selection);
    txtSoLuong.setText(String.valueOf(ct.getSoLuong()));
    txtGia.setText(String.valueOf(ct.getGiaBan()));
    txtMaHoaDon.setText(String.valueOf(ct.getMaHoaDon()));
    jtable.clearSelection();
}//GEN-LAST:event_jTable2MousePressed

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
// TODO add your handling code here:
    int index=selectedRow;
    CTHoaDonPojo ct=dsCTHD.get(index);
   ct.setGiaBan(Double.parseDouble(txtGia.getText()));
   ct.setMaHoaDon(Integer.parseInt(txtMaHoaDon.getText()));
   int index1=combDSSP.getSelectedIndex();
   SanPhamPojo sp=dssp.get(index1);
   ct.setMaSP(sp.getMaSP());
   ct.setSoLuong(Integer.parseInt(txtSoLuong.getText()));
   
   CTHoaDonDao.CapNhatCTHD(ct);
   LoadDSCTHD();
}//GEN-LAST:event_jButton2ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new CapNhatTrangThaiHD().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox combDSSP;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JComboBox jComboBox4;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable2;
    private javax.swing.JTextField txtGia;
    private javax.swing.JTextField txtMaHoaDon;
    private javax.swing.JTextField txtSoLuong;
    // End of variables declaration//GEN-END:variables
}
